package com.haoze.nurseapp.schedule;

import com.gexin.fastjson.JSON;
import com.haoze.common.dto.PushSocketMessage;
import com.haoze.common.dto.SocketMessageDTO;
import com.haoze.common.enumeration.ConstantEnum;
import com.haoze.common.utils.HttpClientUtil;
import com.haoze.common.utils.JSONUtils;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurseapp.dao.TaskExecuteMapper;
import com.haoze.nurseapp.dto.Notification;
import com.haoze.nurseapp.dto.Patient;
import com.haoze.nurseapp.dto.TargetUser;
import com.haoze.nurseapp.service.NurseTeamService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 发送提醒消息
 *
 * @Author: Chenchao
 * @CreateTime: 2020-11-18 14:35
 */
@Component
@ConditionalOnProperty(prefix = "schedule", name = "copyData", havingValue = "true")
public class SendRemindInfoSchedule {

    @Resource
    private NurseTeamService nurseTeamService;
    @Value("${socket.url}")
    private String socketUrl;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    TaskExecuteMapper taskExecuteMapper;


    @Scheduled(cron = "0 */1 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void sendRemindInfoSchedule() {
        List<Map<String, String>> remindInfo = taskExecuteMapper.queryRemindInfo();
        remindInfo.parallelStream().forEach(info -> {
            List<String> accountList = nurseTeamService.querySendUser(info.get("inpatNum"), "nurse");
            List<Notification> list = new CopyOnWriteArrayList<>();
            accountList.parallelStream().forEach(account -> {
                Notification notification = new Notification();
                notification.setNotifyDate(new Date());
                notification.setContent(info.get("messageInfo"));
                notification.setPatient(new Patient(info.get("inpatNum")));
                notification.setType(ConstantEnum.NOTIFICATION_TASK.getKey());
                notification.setContentType(ConstantEnum.NOTIFICATION_TYPE_TEXT.getKey());
                notification.setNotifyLevel("2");
                notification.setId(new ObjectId());
                notification.setNotificationId(UUIDUtil.randomString());
                notification.setTargetUser(new TargetUser(account));
                PushSocketMessage pushSocketMessage = new PushSocketMessage();
                SocketMessageDTO socketMessageDTO = new SocketMessageDTO();
                socketMessageDTO.setData(JSONUtils.jsonToMap(JSONUtils.beanToJson(notification)));
                socketMessageDTO.setMsgType("text");
                pushSocketMessage.setUserIds(Collections.singletonList(account));
                pushSocketMessage.setData(socketMessageDTO);
                HttpClientUtil.sendPost(socketUrl, pushSocketMessage);
                list.add(notification);
            });
            mongoTemplate.insertAll(list);
            taskExecuteMapper.updateRemindInfo(info.get("remindingId"));
        });
    }
}
